package paper.dao.impl;

import java.util.List;

import keter.dao.base.KeterAbstractDao;

import org.springframework.stereotype.Repository;

import paper.dao.TopicDao;
import paper.domain.AuditStatus;
import paper.domain.Topic;

@Repository
public class TopicDaoImpl extends KeterAbstractDao<Topic> implements TopicDao {
    /**
     * <p>Method ：findByTeacher
     * <p>Description : 查找自己维护的课题
     *
     * @param id
     * @return 
     * @see paper.dao.TopicDao#findByTeacher(java.lang.Long)
     */
    @Override
    public List<Topic> findByCreator(Long id) {
       return getResultList("from Topic t where t.creatorId = :id order by created desc", 
              wrap("id"),
              wrap(id));
    }
    
    /**
     * <p>Method ：findByFinisher
     * <p>Description : (学生)查看老师分配给自己完成的课题
     *
     * @param id
     * @return 
     * @see paper.dao.TopicDao#findByFinisher(java.lang.Long)
     */
    @Override
    public List<Topic> findByFinisher(Long id) {
        return getResultList("from Topic t where t.finisher.id = :id", 
                wrap("id"),
                wrap(id));
    }
    
    /**
     * <p>Method ：findByMaster
     * <p>Description : （老师）查看查看自己负责确定的课题
     *
     * @param id
     * @return 
     * @see paper.dao.TopicDao#findSubmitOrPassByMaster(java.lang.Long)
     */
    @Override
    public List<Topic> findSubmitOrPassByMaster(Long id) {
    	//当前用户是负责人且状态为“提交”的课题
        return getResultList("from Topic t "
        		+ "where t.master.id = :id "
        		+ "and t.status='SUBMIT' or t.status='PASS' "
        		+ "order by t.submitted desc", 
				wrap("id"),
                wrap(id));
    }

    /**
     * <p>Method ：submitTopic
     * <p>Description : (学生)提交课题
     *
     * @param id 
     * @see paper.dao.TopicDao#submitTopic(java.lang.Long)
     */
    @Override
    public void submitTopic(Topic topic) {
        topic.setStatus(AuditStatus.SUBMIT.toString());
        save(topic);
    }

	@Override
	public void deleteTopic(Long id) {
		executeUpdate("delete from Topic t where t.id=:id", 
                wrap("id"),
                wrap(id)
        );
	}
}